package com.qk.management.task;

import com.qk.common.util.DeleteOSSFiles;
import com.qk.common.util.GetOSSAllFiles;
import com.qk.management.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author kaoM1Jy
 * @date 2025/10/11 20:33
 * @description:
 */
@Component
public class FileTask {
    @Autowired
    private GetOSSAllFiles getOSSAllFiles;
    @Autowired
    private DeleteOSSFiles deleteOSSFiles;
    @Autowired
    private UserMapper userMapper;

    @Scheduled(cron = "0 0 * * 1/1 ?  ")
    public void clean() throws Exception {
        List<String> ossFiles = getOSSAllFiles.listAllObjects();
        List<String> dbRecentImages = userMapper.selectRecentImages();
        List<String> filesToDelete = ossFiles.stream()
                .filter(file -> !dbRecentImages.contains(file))
                .collect(Collectors.toList());

        if (!filesToDelete.isEmpty()) {
            deleteOSSFiles.DeleteFiles(filesToDelete);

        }
    }
}
